<?php
class Admin_setting_Controller extends Template_Controller {

	public $search;
	public $template = 'admin/index';	
	
    public function __construct()
    {
        parent::__construct();
        
        $this->search = array('keyword' => '','page' => 0);
        
        $this->_get_submit();
    }
    
	public function __call($method, $arguments)
	{
		// Disable auto-rendering
		$this->auto_render = FALSE;
	}
	
	function _get_submit()
	{
		if($this->session->get('error_msg'))
			$this->template->error_msg = $this->session->get('error_msg');
		if($this->session->get('warning_msg'))
			$this->template->warning_msg = $this->session->get('warning_msg');
		if($this->session->get('success_msg'))
			$this->template->success_msg = $this->session->get('success_msg');
		if($this->session->get('info_msg'))
			$this->template->info_msg = $this->session->get('info_msg');
		if($this->session->get('frm')){			
			$this->template->content->mr = $this->session->get('frm');
		}
	}
	
	public function index()
	{		
		$this->template->content = new View('admin_setting/index');
		
		$model = new Site_Model();
		$this->template->content->mr = $model->get();
		
		$this->_get_submit();
	}
	
    private function _set_form_cash($form)
    {	
		$record['payments_method_code'] = $form['txt_cash_code'];
		$record['payments_method_name'] = $form['txt_cash_name'];		
		$record['payments_method_sort_order'] = $form['txt_cash_sort_order'];
		$record['payments_method_status'] = $form['sel_cash_status'];		
		return $record;
	}
    
    private function _set_form_po($form)
    {	
		$record['payments_method_code'] = $form['txt_po_code'];
		$record['payments_method_name'] = $form['txt_po_name'];		
		$record['payments_method_sort_order'] = $form['txt_po_sort_order'];
		$record['payments_method_status'] = $form['sel_po_status'];		
		return $record;
	}
    
    private function _set_form_aut($form)
    {	
		$record['payments_method_code'] = $form['txt_aut_code'];
		$record['payments_method_name'] = $form['txt_aut_name'];		
		$record['payments_method_sort_order'] = $form['txt_aut_sort_order'];
		$record['payments_method_status'] = $form['sel_aut_status'];		
		return $record;
	}
    
    private function _set_form_aut_config($form)
    {	
		$record['api_login'] = $form['txt_aut_api_login'];
		$record['transaction_key'] = $form['txt_aut_transaction_key'];
		$record['post_url'] = $form['sel_aut_post_url'];				
		return $record;
	}
    
    private function _set_form_free($form)
    {	
		$record['shipping_method_code'] = $form['txt_free_code'];
		$record['shipping_method_name'] = $form['txt_free_name'];		
		$record['shipping_method_sort_order'] = $form['txt_free_sort_order'];
		$record['shipping_method_status'] = $form['sel_free_status'];		
		return $record;
	}
    
    private function _set_form_fedex($form)
    {	
		$record['shipping_method_code'] = $form['txt_fedex_code'];
		$record['shipping_method_name'] = $form['txt_fedex_name'];		
		$record['shipping_method_sort_order'] = $form['txt_fedex_sort_order'];
		$record['shipping_method_status'] = $form['sel_fedex_status'];		
		return $record;
	}
    
    private function _set_form_ups($form)
    {	
		$record['shipping_method_code'] = $form['txt_ups_code'];
		$record['shipping_method_name'] = $form['txt_ups_name'];		
		$record['shipping_method_sort_order'] = $form['txt_ups_sort_order'];
		$record['shipping_method_status'] = $form['sel_ups_status'];		
		return $record;
	}
    
    private function _set_form_usps($form)
    {	
		$record['shipping_method_code'] = $form['txt_usps_code'];
		$record['shipping_method_name'] = $form['txt_usps_name'];		
		$record['shipping_method_sort_order'] = $form['txt_usps_sort_order'];
		$record['shipping_method_status'] = $form['sel_usps_status'];		
		return $record;
	}
    
    private function _set_form_fedex_config($form)
    {	
		$record['key'] = $form['txt_fedex_key'];
		$record['password'] = $form['txt_fedex_password'];
		$record['account_number'] = $form['txt_fedex_account_number'];				
		$record['meter_number'] = $form['txt_fedex_meter_number'];
		//$record['service_type'] = $form['sel_fedex_service_type'];
		return $record;
	}
    
    private function _set_form_ups_config($form)
    {	
		$record['developer_key'] = $form['txt_ups_developer_key'];
		$record['access_key'] = $form['txt_ups_access_key'];
		$record['username'] = $form['txt_ups_username'];
		$record['password'] = $form['txt_ups_password'];
		//$record['service_type'] = $form['sel_ups_service_type'];		
		$record['server'] = $form['sel_ups_server'];
		return $record;
	}
    
    private function _set_form_usps_config($form)
    {	
		$record['username'] = $form['txt_usps_username'];
		$record['password'] = $form['txt_usps_password'];
		//$record['service_type'] = $form['sel_usps_service_type'];
		$record['post_url'] = $form['sel_usps_post_url'];
		return $record;
	}
    
    private function _get_record_cash()
    {
    	$form = array
	    (
	    	'txt_cash_code' => '',
	        'txt_cash_name' => '',	        
	        'txt_cash_sort_order' => '',
	        'sel_cash_status' => '',	        
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_cash_name','required');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form_cash($form);
			if($post->validate())
 			{
 				return $form; 				
			} else {
				$this->session->set_flash('frm_aut',$form);
				$errors = arr::overwrite($errors, $post->errors('payment_method_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				url::redirect('admin_setting/setup');
				die();
			}
        }
    }
    
    private function _get_record_aut()
    {
    	$form = array
	    (
	    	'txt_aut_code' => '',
	        'txt_aut_name' => '',	        
	        'txt_aut_sort_order' => '',
	        'sel_aut_status' => '',	        
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_aut_name','required');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form_aut($form);
			if($post->validate())
 			{
 				return $form; 				
			} else {
				$this->session->set_flash('frm_cash',$form);
				$errors = arr::overwrite($errors, $post->errors('payment_method_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				url::redirect('admin_setting/setup');
				die();
			}
        }
    }
    
    private function _get_record_po()
    {
    	$form = array
	    (
	    	'txt_po_code' => '',
	        'txt_po_name' => '',	        
	        'txt_po_sort_order' => '',
	        'sel_po_status' => '',	        
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_po_name','required');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form_po($form);
			if($post->validate())
 			{
 				return $form; 				
			} else {
				$this->session->set_flash('frm_po',$form);
				$errors = arr::overwrite($errors, $post->errors('payment_method_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				url::redirect('admin_setting/setup');
				die();
			}
        }
    }
    
    private function _get_record_free()
    {
    	$form = array
	    (
	    	'txt_free_code' => '',
	        'txt_free_name' => '',	        
	        'txt_free_sort_order' => '',
	        'sel_free_status' => '',	        
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_free_name','required');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form_free($form);
			if($post->validate())
 			{
 				return $form; 				
			} else {
				$this->session->set_flash('frm_free',$form);
				$errors = arr::overwrite($errors, $post->errors('shipping_method_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				url::redirect('admin_setting/setup');
				die();
			}
        }
    }
    
    private function _get_record_fedex()
    {
    	$form = array
	    (
	    	'txt_fedex_code' => '',
	        'txt_fedex_name' => '',	        
	        'txt_fedex_sort_order' => '',
	        'sel_fedex_status' => '',	        
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_fedex_name','required');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form_fedex($form);
			if($post->validate())
 			{
 				return $form; 				
			} else {
				$this->session->set_flash('frm_fedex',$form);
				$errors = arr::overwrite($errors, $post->errors('shipping_method_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				url::redirect('admin_setting/setup');
				die();
			}
        }
    }
    
    private function _get_record_ups()
    {
    	$form = array
	    (
	    	'txt_ups_code' => '',
	        'txt_ups_name' => '',	        
	        'txt_ups_sort_order' => '',
	        'sel_ups_status' => '',	        
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_ups_name','required');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form_ups($form);
			if($post->validate())
 			{
 				return $form; 				
			} else {
				$this->session->set_flash('frm_ups',$form);
				$errors = arr::overwrite($errors, $post->errors('shipping_method_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				url::redirect('admin_setting/setup');
				die();
			}
        }
    }
    
    private function _get_record_usps()
    {
    	$form = array
	    (
	    	'txt_usps_code' => '',
	        'txt_usps_name' => '',	        
	        'txt_usps_sort_order' => '',
	        'sel_usps_status' => '',	        
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_usps_name','required');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form_usps($form);
			if($post->validate())
 			{
 				return $form; 				
			} else {
				$this->session->set_flash('frm_usps',$form);
				$errors = arr::overwrite($errors, $post->errors('shipping_method_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				url::redirect('admin_setting/setup');
				die();
			}
        }
    }
    
    private function _get_record_aut_config()
    {
    	$form = array
	    (
	        'txt_aut_api_login' => '',
	        'txt_aut_transaction_key' => '',
	        'sel_aut_post_url' => '',	        
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_aut_api_login','trim','required');
			$post->add_rules('txt_aut_transaction_key','trim','required');
			$post->add_rules('sel_aut_post_url','trim','required');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form_aut_config($form);
			if($post->validate())
 			{
 				return $form; 				
			} else {
				$this->session->set_flash('frm_aut',$form);
				$errors = arr::overwrite($errors, $post->errors('authorizenet_config_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				url::redirect('admin_setting/setup');
				die();
			}
        }
    }
    
    private function _get_record_fedex_config()
    {
    	$form = array
	    (
	        'txt_fedex_key' => '',
	        'txt_fedex_password' => '',
	        'txt_fedex_account_number' => '',
			'txt_fedex_meter_number' => '',
			//'sel_fedex_service_type' => '',      
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_fedex_key','trim','required');
			$post->add_rules('txt_fedex_password','trim','required');
			$post->add_rules('txt_fedex_account_number','trim','required');
			$post->add_rules('txt_fedex_meter_number','trim','required');
			//$post->add_rules('sel_fedex_service_type','trim','required');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form_fedex_config($form);
			if($post->validate())
 			{
 				return $form; 				
			} else {
				$this->session->set_flash('frm_fedex',$form);
				$errors = arr::overwrite($errors, $post->errors('fedex_config_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				url::redirect('admin_setting/setup');
				die();
			}
        }
    }    
    
    private function _get_record_ups_config()
    {
    	$form = array
	    (
	        'txt_ups_developer_key' => '',
	        'txt_ups_access_key' => '',
	        'txt_ups_username' => '',
	        'txt_ups_password' => '',
	        //'sel_ups_service_type' => '',
			'sel_ups_server' => '',      
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_ups_developer_key','trim','required');
			$post->add_rules('txt_ups_access_key','trim','required');
			$post->add_rules('txt_ups_username','trim','required');
			$post->add_rules('txt_ups_password','trim','required');
			//$post->add_rules('sel_ups_service_type','trim','required');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form_ups_config($form);
			if($post->validate())
 			{
 				return $form;
			} else {
				$this->session->set_flash('frm',$form);
				$errors = arr::overwrite($errors, $post->errors('ups_config_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				url::redirect('admin_setting/setup');
				die();
			}
        }
    }
    
    private function _get_record_usps_config()
    {
    	$form = array
	    (
	        'txt_usps_username' => '',
	        'txt_usps_password' => '',
	        //'sel_usps_service_type' => '',
			'sel_usps_post_url' => '',      
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_usps_username','trim','required');
			$post->add_rules('txt_usps_password','trim','required');
			//$post->add_rules('sel_usps_service_type','trim','required');
			$post->add_rules('sel_usps_post_url','trim','required');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form_usps_config($form);
			if($post->validate())
 			{
 				return $form; 				
			} else {
				$this->session->set_flash('frm',$form);
				$errors = arr::overwrite($errors, $post->errors('usps_config_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				url::redirect('admin_setting/setup');
				die();
			}
        }
    }
    
	private function _get_record()
    {
    	$form = array
	    (
	        'txt_name' => '',
            'txt_slogan' => '',
	        'txt_address' => '',
	        'txt_address2' => '',
	        'txt_city' => '',
			'txt_state' => '',
			'txt_zipcode' => '',      
	        'txt_phone' => '',
	        'txt_fax' => '',	        
	        'txt_email' => '',
	        'txt_email2' => '',
	        'txt_title' => '',
	        'txt_keyword' => '',
	        'txt_footer' => '',		       
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_name','required');
			$post->add_rules('txt_address','trim');
			$post->add_rules('txt_address2','trim');
			$post->add_rules('txt_city','trim');
			$post->add_rules('txt_state','trim');
			$post->add_rules('txt_zipcode','trim');
			$post->add_rules('txt_phone','trim');
			$post->add_rules('txt_fax','trim');
			$post->add_rules('txt_email','trim','email');
			$post->add_rules('txt_email2','trim','email');
			$post->add_rules('txt_title','trim');
			$post->add_rules('txt_keyword','trim');
			$post->add_rules('txt_footer','trim');
			if($post->validate())
 			{
 				$form = arr::overwrite($form, $post->as_array());
 				return $form; 				
			} else {
				$errors = arr::overwrite($errors, $post->errors('site_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);				
			}
        }
    }
	
	public function index_sm()
	{
		$record = $this->_get_record();
		
		if($record)
		{
			$site['site_name'] = $record['txt_name'];
            $site['site_slogan'] = $record['txt_slogan'];
			$site['site_address'] = $record['txt_address'];
			$site['site_address2'] = $record['txt_address2'];
			$site['site_city'] = $record['txt_city'];
			$site['site_state'] = $record['txt_state'];
			$site['site_zipcode'] = $record['txt_zipcode'];
			$site['site_phone'] = $record['txt_phone'];
			$site['site_fax'] = $record['txt_fax'];
			$site['site_email'] = $record['txt_email'];
			$site['site_email2'] = $record['txt_email2'];
			$site['site_title'] = $record['txt_title'];
			$site['site_keyword']= $record['txt_keyword'];
			$site['site_footer'] = $record['txt_footer'];
			$result = $this->db->update('site',$site,array('site_name'=>$this->input->post('site_name')));			
			if ($result)				
				$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_save'));
			else $this->session->set_flash('error_msg',Kohana::lang('errormsg_lang.msg_query_fail'));
		}
		url::redirect('admin_setting');
		die();
	}
	
	public function setup()
	{
		$this->template->content = new View('admin_setting/setup');	
		
		$model = new Site_Model();
		$this->template->content->mr = $model->get();
		//Authorize.net
		$mr_aut = ORM::factory('payment_method')->where('payments_method_code', 'AUT')->find()->as_array();	
    	$model = new Authorizenet_config_Model();
    	$mr_temp = $model->get(); 
    	$mr_aut = array_merge($mr_temp,$mr_aut);
    	$this->template->content->mr_aut = $mr_aut;
    	//Cash
		$mr_cash = ORM::factory('payment_method')->where('payments_method_code', 'CMO')->find()->as_array();	
    	$this->template->content->mr_cash = $mr_cash;
    	//PO
		$mr_po = ORM::factory('payment_method')->where('payments_method_code', 'PO')->find()->as_array();
    	$this->template->content->mr_po = $mr_po;
    	
    	//Free
    	$mr_free = ORM::factory('shipping_method')->where('shipping_method_code', 'FREE')->find()->as_array();
    	$this->template->content->mr_free = $mr_free;
    	//Fedex
    	$mr_fedex = ORM::factory('shipping_method')->where('shipping_method_code', 'FEDEX')->find()->as_array();
    	$model = new Fedex_config_Model();
    	$mr_temp = $model->get();
    	$mr_fedex = array_merge($mr_temp,$mr_fedex);
    	$this->template->content->mr_fedex = $mr_fedex;
    	//UPS
    	$mr_ups = ORM::factory('shipping_method')->where('shipping_method_code', 'UPS')->find()->as_array();
    	$model = new Ups_config_Model();
    	$mr_temp = $model->get();
    	$mr_ups = array_merge($mr_temp,$mr_ups);
    	$this->template->content->mr_ups = $mr_ups;
    	//USPS
    	$mr_usps = ORM::factory('shipping_method')->where('shipping_method_code', 'USPS')->find()->as_array();
    	$model = new Usps_config_Model();
    	$mr_temp = $model->get();
    	$mr_usps = array_merge($mr_temp,$mr_usps);
    	$this->template->content->mr_usps = $mr_usps;
    	
		$this->_get_submit();
	}
	
	private function _get_record_setup()
	{
		$form = array
	    (
	        'txt_num_line' => '',
	        'txt_num_line2' => '',
			'txt_short_date' => '',
			'txt_long_date' => '',
			'txt_rma_format' => '',
			'txt_promotion_format' => '', 
			'rdo_currency' => '',
			'rdo_unit' => '',
			'rdo_length_unit' => '',
			//'rdo_lang' => '',
			//'rdo_lang_admin' => '',			   	             
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_num_line','required','digit');
			$post->add_rules('txt_num_line2','required','digit');			
			$post->add_rules('txt_short_date','required');
			$post->add_rules('txt_long_date','required');						
			$post->add_rules('txt_rma_format','required');
			$post->add_rules('rdo_currency','required');
			$post->add_rules('rdo_unit','required');
			$post->add_rules('rdo_length_unit','required');
			//$post->add_rules('rdo_lang','required');
			//$post->add_rules('rdo_lang_admin','required');
			if($post->validate())
 			{
 				$form = arr::overwrite($form, $post->as_array());
 				return $form; 				
			} else {
				$errors = arr::overwrite($errors, $post->errors('site_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);	
				
				url::redirect('admin_setting/setup');			
			}
        }
	}
	
	private function _update_free()
	{
		$record = $this->_get_record_free();
		$query = $this->db->update('shipping_method', $record, array('shipping_method_id' => $this->input->post('hd_free_id')));
	}
	
	private function _update_fedex()
	{
		$record = $this->_get_record_fedex();
		$query = $this->db->update('shipping_method', $record, array('shipping_method_id' => $this->input->post('hd_fedex_id')));
		
		$record = $this->_get_record_fedex_config();
		$query = $this->db->update('fedex_config', $record, array('key' => $this->input->post('hd_fedex_key')));
	}
	
	private function _update_ups()
	{
		$record = $this->_get_record_ups();
		$query = $this->db->update('shipping_method', $record, array('shipping_method_id' => $this->input->post('hd_ups_id')));
		
		$record = $this->_get_record_ups_config();
		$query = $this->db->update('ups_config', $record, array('developer_key' => $this->input->post('hd_ups_developer_key')));
	}
	
	private function _update_usps()
	{
		$record = $this->_get_record_usps();
		$query = $this->db->update('shipping_method', $record, array('shipping_method_id' => $this->input->post('hd_usps_id')));
		
		$record = $this->_get_record_usps_config();
		$query = $this->db->update('usps_config', $record, array('username' => $this->input->post('hd_usps_username')));
	}
	
	private function _update_cash()
	{
		$record = $this->_get_record_cash();
		$query = $this->db->update('payments_method', $record, array('payments_method_id' => $this->input->post('hd_cash_id')));
	}
	
	private function _update_po()
	{
		$record = $this->_get_record_po();
		$query = $this->db->update('payments_method', $record, array('payments_method_id' => $this->input->post('hd_po_id')));
	}
	
	private function _update_aut()
	{
		$record = $this->_get_record_aut();
		$query = $this->db->update('payments_method', $record, array('payments_method_id' => $this->input->post('hd_aut_id')));
		
		$record = $this->_get_record_aut_config();
		$query = $this->db->update('authorizenet_config', $record, array('api_login' => $this->input->post('hd_aut_api_login')));
	}
	
	public function setup_sm()
	{
		$hd_name = $this->input->post('hd_name');
		$record = $this->_get_record_setup();
		
		if($record)
		{
			//Site
			$site['site_num_line'] = $record['txt_num_line'];
			$site['site_num_line2'] = $record['txt_num_line2'];		
			$site['site_short_date'] = $record['txt_short_date'];
			$site['site_long_date'] = $record['txt_long_date'];
			$site['site_rma_format'] = $record['txt_rma_format'];
			$site['site_promotion_format'] = $record['txt_promotion_format'];	
			$site['site_currency'] = $record['rdo_currency'];	
			$site['site_unit'] = $record['rdo_unit'];
			$site['site_length_unit'] = $record['rdo_length_unit'];
			//$site['site_lang_client'] = $record['rdo_lang'];	
			//$site['site_lang_admin'] = $record['rdo_lang_admin'];	
			$result = $this->db->update('site',$site,array('site_name'=>$hd_name));
			//Cash
			$this->_update_cash();
			//PO
			$this->_update_po();
			//Authorize.net
			$this->_update_aut();
			//Free
			$this->_update_free();			
			//Fedex
			$this->_update_fedex();
			//Ups
			$this->_update_ups();
			//Usps
			$this->_update_usps();
			if($result)				
				$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_save'));	
			else $this->session->set_flash('error_msg',Kohana::lang('errormsg_lang.msg_query_fail'));
		}		
		url::redirect('admin_setting/setup');
		die();
	}
}
?>